package com.leapp.sk.viewdelegate.personal

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.GridView
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lklibrary.log.LogOperate
import com.example.inter.ItemClickInterface
import com.example.mvp.adapter.UPImageAdapter
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.mvp.view.NoScrollGridView
import com.example.mvp.view.SKTopBar
import com.example.mvp.view.imgselector.ImageSelector
import com.leapp.sk.R
import com.leapp.sk.ui.activity.personal.FeedBackDetialActicity
import com.leapp.sk.ui.activity.personal.FeedProblemActiviaty

/**
 * 反馈问题视图托管
 * @author  chaohaipeng  on 2018/8/9.
 */
class FeedProblemDelegate :BaseAppDelegate(){

    private var tv_afp_function:TextView?=null
    private var tv_afp_proposal:TextView?=null
    private var tv_afp_content:TextView?=null
    private var tv_afp_other:TextView?=null

   private var upImageAdapter:UPImageAdapter?=null
    /**
     * 问题类别
     */
    private var type=""
    /**
     * 当前图片数量
     */
    private var currentImageNum:Int=0

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_feed_problem
    }

    /**
     * 初始化适配器
     */
    fun initImageAdapter(context: Context, mlist: List<String>, num:Int,itemClickInterface: ItemClickInterface){
        val gv_afp_up_image = getView<NoScrollGridView>(R.id.gv_afp_up_image)
         upImageAdapter = UPImageAdapter(context, mlist, num,itemClickInterface)
        gv_afp_up_image.adapter=upImageAdapter
        horizontal_layout(4)
        gv_afp_up_image.setOnItemClickListener{adapterView, view, i, l ->
            LogOperate.e("位置"+i)
            if(i==currentImageNum){
                pickImage(context)
            }
        }
        inittype()
    }

    /**
     * 初始化类别
     */
   fun inittype(){
        tv_afp_function = getView<TextView>(R.id.tv_afp_function)
        tv_afp_proposal = getView<TextView>(R.id.tv_afp_proposal)
        tv_afp_content = getView<TextView>(R.id.tv_afp_content)
        tv_afp_other = getView<TextView>(R.id.tv_afp_other)
       setType(true,false,false,false)
       initEdit()
   }
    /**
     * 设置底部类别
     */
    fun setType(function:Boolean,proposal:Boolean,content:Boolean,other:Boolean){
       if (function){
           type="EXCEPTION"
           tv_afp_function?.isSelected=true
           tv_afp_proposal?.isSelected=false
           tv_afp_content?.isSelected=false
           tv_afp_other?.isSelected=false
       }else if (proposal){
           type="PRODUCT_ADVISE"
           tv_afp_function?.isSelected=false
           tv_afp_proposal?.isSelected=true
           tv_afp_content?.isSelected=false
           tv_afp_other?.isSelected=false
       }else if (content){
           type="CONTENT_ADVISE"
           tv_afp_function?.isSelected=false
           tv_afp_proposal?.isSelected=false
           tv_afp_content?.isSelected=true
           tv_afp_other?.isSelected=false
       }else if (other){
           type="OTHER_OTHER"
           tv_afp_function?.isSelected=false
           tv_afp_proposal?.isSelected=false
           tv_afp_content?.isSelected=false
           tv_afp_other?.isSelected=true
       }
    }

    /**
     * 获取问题类别
     */
    fun getProblemType():String{
        return type
    }

    /**
     * 获取文本
     */
  fun  getProblemText():String{
     val et_afp_content = getView<ContainsEmojiEditText>(R.id.et_afp_content)
      val trim = et_afp_content.text.toString().trim()
      return trim
  }
    /**
     * 获取邮箱文本
     */
    fun  getEmileText():String{
        val et_afp_qq_emil = getView<ContainsEmojiEditText>(R.id.et_afp_qq_emil)
        val trim = et_afp_qq_emil.text.toString().trim()
        return trim
    }
    /**
     * 获取文本
     */
    fun  initEdit(){
        val et_afp_content = getView<ContainsEmojiEditText>(R.id.et_afp_content)
        val tv_afp_text_num = getView<TextView>(R.id.tv_afp_text_num)
        et_afp_content.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                val length = et_afp_content?.text?.length
                tv_afp_text_num.text="$length/${500}"
            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        })
    }

    /**
     * 头像选择
     */
    private fun pickImage(context: Context) {
        var count = 0
        if (upImageAdapter!=null){
            count=upImageAdapter!!.count
        }
        ImageSelector.getInstance()
                .setSelectModel(ImageSelector.MULTI_MODE)
                .setMaxCount(4-(count-1))
                .setGridColumns(3)
                .setShowCamera(true)
                .startSelect(context)
    }
    /**
     * 刷新适配器
     */
   fun notifyAdapter( num:Int){
        this.currentImageNum=num
        upImageAdapter?.notifyDataSetChanged()
   }
    /**
     * 设置横向的ScrollView 显示图片大小
     */
    fun horizontal_layout(size:Int) {
        val gridView = getView<NoScrollGridView>(R.id.gv_afp_up_image)
        val allWidth = LKScreenUtil.dp2px( 110f)*size
        val itemWidth = LKScreenUtil.dp2px( 110f)
        val params = LinearLayout.LayoutParams(
                allWidth, LinearLayout.LayoutParams.MATCH_PARENT)
        gridView.setLayoutParams(params)// 设置GirdView布局参数
        gridView.setColumnWidth(itemWidth)// 列表项宽
        gridView.setStretchMode(GridView.NO_STRETCH)
    }
    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        val jtb_afbd= getView<SKTopBar>(R.id.jtb_afp)
        if (!TextUtils.isEmpty(title)){
            jtb_afbd.setTitle(title)
        }
        setEvent(jtb_afbd)
    }

    /**
     * 设置设置事件
     */
    fun setEvent(topBar: SKTopBar){
        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<FeedProblemActiviaty>().finish()

            }
            override fun rightOnClick() {
            }

        })
    }
}